package cn.iocoder.yudao.module.ride.service.memberlevel;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ride.controller.admin.memberlevel.vo.*;
import cn.iocoder.yudao.module.ride.dal.dataobject.memberlevel.MemberLevelDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ride.dal.mysql.memberlevel.MemberLevelMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.ride.enums.ErrorCodeConstants.MEMBER_LEVEL_NOT_EXISTS;
import static cn.iocoder.yudao.module.ride.enums.ErrorCodeConstants.*;

/**
 * 会员等级管理 Service 实现类
 *
 * @author tong
 */
@Service
@Validated
public class MemberLevelServiceImpl implements MemberLevelService {

    @Resource
    private MemberLevelMapper memberLevelMapper;

    @Override
    public Integer createMemberLevel(MemberLevelSaveReqVO createReqVO) {
        // 插入
        MemberLevelDO memberLevel = BeanUtils.toBean(createReqVO, MemberLevelDO.class);
        memberLevelMapper.insert(memberLevel);

        // 返回
        return memberLevel.getId();
    }

    @Override
    public void updateMemberLevel(MemberLevelSaveReqVO updateReqVO) {
        // 校验存在
        validateMemberLevelExists(updateReqVO.getId());
        // 更新
        MemberLevelDO updateObj = BeanUtils.toBean(updateReqVO, MemberLevelDO.class);
        memberLevelMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberLevel(Integer id) {
        // 校验存在
        validateMemberLevelExists(id);
        // 删除
        memberLevelMapper.deleteById(id);
    }

    @Override
        public void deleteMemberLevelListByIds(List<Integer> ids) {
        // 删除
        memberLevelMapper.deleteByIds(ids);
        }


    private void validateMemberLevelExists(Integer id) {
        if (memberLevelMapper.selectById(id) == null) {
            throw exception(MEMBER_LEVEL_NOT_EXISTS);
        }
    }

    @Override
    public MemberLevelDO getMemberLevel(Integer id) {
        return memberLevelMapper.selectById(id);
    }

    @Override
    public PageResult<MemberLevelDO> getMemberLevelPage(MemberLevelPageReqVO pageReqVO) {
        return memberLevelMapper.selectPage(pageReqVO);
    }

}